#!/usr/bin/env python
#
# Copyright (C) 2009 Dody Suria Wijaya.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Contains extensions to Atom objects used with Ajax Feed.
"""


__author__ = 'dodysw@gmail.com (Dody Suria Wijaya)'


import simplejson
import atom.core
import ajaxfeed.data

def GAjaxFeedFindFromResponse(response):
    return GAjaxFeedFindFromString(response.read())

def GAjaxFeedFindFromString(json_string):
    results = simplejson.loads(json_string)
    if results['responseData']:
        data = results['responseData']
        entries = []
        for entry in data.get('entries', []):
            entries.append(ajaxfeed.data.FindFeedEntry(
                title=entry.get('title',''),
                url=entry.get('url',''),
                link=entry.get('link',''),
                contentSnippet=entry.get('contentSnippet','')))

        return ajaxfeed.data.FindFeed(query=data['query'], entries=entries
            ).SetResponseStatusDetail(results['responseStatus'],
                results['responseDetails'])
    else:
        return ajaxfeed.data.Error().SetResponseStatusDetail(
            results['responseStatus'], results['responseDetails'])

def GAjaxFeedLookupFromResponse(response):
    return GAjaxFeedLookupFromString(response.read())

def GAjaxFeedLookupFromString(json_string):
    results = simplejson.loads(json_string)
    if results['responseData']:
        data = results['responseData']
        return ajaxfeed.data.LookupFeed(query=data['query'], url=data['url']
            ).SetResponseStatusDetail(results['responseStatus'],
                results['responseDetails'])
    else:
        return ajaxfeed.data.Error().SetResponseStatusDetail(
            results['responseStatus'], results['responseDetails'])

def GAjaxFeedLoadFromResponse(response):
    return GAjaxFeedLoadFromString(response.read())

def GAjaxFeedLoadFromString(json_string):
    results = simplejson.loads(json_string)
    if results['responseData']:
        data = results['responseData']
        feed = data['feed']
        entries = []
        for entry in feed.get('entries', []):
            entries.append(ajaxfeed.data.FeedEntry(
                title=entry.get('title',''),
                link=entry.get('link',''),
                author=entry.get('author',''),
                categories=entry.get('categories',''),
                content=entry.get('content',''),
                publishedDate=entry.get('publishedDate',''),
                contentSnippet=entry.get('contentSnippet',''),
                ))

        return ajaxfeed.data.Feed(
            author=feed.get('author',''),
            description=feed.get('description',''),
            link=feed.get('link',''),
            title=feed.get('title',''),
            type=feed.get('type',''),
            entries=entries
            ).SetResponseStatusDetail(results['responseStatus'],
                results['responseDetails'])
    else:
        return ajaxfeed.data.Error().SetResponseStatusDetail(
            results['responseStatus'], results['responseDetails'])
